/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLO;

import DAO.ProjectDAO;
import DTO.Members;
import DTO.Minestones;
import DTO.Project;
import DTO.Projects;
import DTO.Task;
import DTO.Tasks;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import util.SupportUtil;

/**
 *
 * @author ThanhTai
 */
public class ProjectBLO {

    private List<Integer> projectIDList;
    private List<Integer> minestoneIDList;

    public ProjectBLO() {
    }

    public ProjectBLO(List<Integer> projectIDList, List<Integer> MinestoneIDList) {
        this.projectIDList = projectIDList;
        this.minestoneIDList = MinestoneIDList;
    }

    public void getAllProject(String xmlFilePath) {
        try {
            ProjectDAO dao = new ProjectDAO();
            Projects projects = dao.GetAllProject(null);

            for (int i = 0; i < projects.size(); i++) {
                Project object = projects.get(i);
                projectIDList.add(object.getProjectID());
            }
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, projects);

//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document doc = db.parse(new File(xmlFilePath));
//            Node projectsNode = (doc.getElementsByTagName("projects")).item(0);
//
//            //Delete old child
//            NodeList children = projectsNode.getChildNodes();
//            for (int i = 0; i < children.getLength(); i++) {
//                if (children.item(i).getNodeName().equals("project")) {
//                    projectsNode.removeChild(children.item(i));
//                }
//            }
//
//            //Write data into xml file
//            for (int i = 0; i < projects.size(); i++) {
//                DTO.Project project = projects.get(i);
//                //Create element for project tag
//                Element ProjectElement = doc.createElement("project");
//                Element ProjectIDElement = doc.createElement("projectID");
//                Element ProjectNameElement = doc.createElement("projectName");
//                Element ProjectManagerElement = doc.createElement("projectManager");
//                Element DescriptionElement = doc.createElement("description");
//                Element NumofMinestoneElement = doc.createElement("numofMinestone");
//
//                Element StartDayElement = doc.createElement("startDay");
//                Element sdYearElement = doc.createElement("year");
//                Element sdMonthElement = doc.createElement("month");
//                Element sdDayElement = doc.createElement("day");
//
//                Element EndDayElement = doc.createElement("endDay");
//                Element seYearElement = doc.createElement("year");
//                Element seMonthElement = doc.createElement("month");
//                Element seDayElement = doc.createElement("day");
//
//                Element StatusElement = doc.createElement("status");
//
//                //Append text for element of project's info
//                ProjectIDElement.appendChild(doc.createTextNode(project.getProjectID() + ""));
//                ProjectNameElement.appendChild(doc.createTextNode(project.getProjectName() + ""));
//                ProjectManagerElement.appendChild(doc.createTextNode(project.getProjectManager() + ""));
//                DescriptionElement.appendChild(doc.createTextNode(project.getDescription() + ""));
//                NumofMinestoneElement.appendChild(doc.createTextNode(project.getNumOfMinestone() + ""));
//
//                sdYearElement.appendChild(doc.createTextNode(project.getStartDay().getYear() + ""));
//                sdMonthElement.appendChild(doc.createTextNode(project.getStartDay().getMonth() + ""));
//                sdDayElement.appendChild(doc.createTextNode(project.getStartDay().getDay() + ""));
//                StartDayElement.appendChild(sdYearElement);
//                StartDayElement.appendChild(sdMonthElement);
//                StartDayElement.appendChild(sdDayElement);
//
//                seYearElement.appendChild(doc.createTextNode(project.getEndDay().getYear() + ""));
//                seMonthElement.appendChild(doc.createTextNode(project.getEndDay().getMonth() + ""));
//                seDayElement.appendChild(doc.createTextNode(project.getEndDay().getDay() + ""));
//                EndDayElement.appendChild(seYearElement);
//                EndDayElement.appendChild(seMonthElement);
//                EndDayElement.appendChild(seDayElement);
//
//                StatusElement.appendChild(doc.createTextNode(project.getStatus() + ""));
//
//                //Append child
//                ProjectElement.appendChild(ProjectIDElement);
//                ProjectElement.appendChild(ProjectNameElement);
//                ProjectElement.appendChild(ProjectManagerElement);
//                ProjectElement.appendChild(DescriptionElement);
//                ProjectElement.appendChild(NumofMinestoneElement);
//                ProjectElement.appendChild(StartDayElement);
//                ProjectElement.appendChild(EndDayElement);
//                ProjectElement.appendChild(StatusElement);
//
//                //Append child for projects tag
//                projectsNode.appendChild(ProjectElement);
//                TransformerUtil.writeXML(doc, xmlFilePath);
//                projectIDList.add(project.getProjectID());
//            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllProjectOfProjectManager(String xmlFilePath, String username) {
        try {
            projectIDList = new ArrayList<Integer>();
            String condition = " WHERE ProjectManager = '" + username + "'";
            ProjectDAO dao = new ProjectDAO();
            Projects projects = dao.GetAllProject(condition);
            for (int i = 0; i < projects.size(); i++) {
                Project object = projects.get(i);
                projectIDList.add(object.getProjectID());
            }
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, projects);
//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document doc = db.parse(new File(xmlFilePath));
//            Node projectsNode = (doc.getElementsByTagName("projects")).item(0);
//
//            //Delete old child
//            NodeList children = projectsNode.getChildNodes();
//            for (int i = 0; i < children.getLength(); i++) {
//                if (children.item(i).getNodeName().equals("project")) {
//                    projectsNode.removeChild(children.item(i));
//                }
//            }
//
//            //Write data into xml file
//            for (int i = 0; i < projects.size(); i++) {
//                DTO.Project project = projects.get(i);
//                //Create element for project tag
//                Element ProjectElement = doc.createElement("project");
//                Element ProjectIDElement = doc.createElement("projectID");
//                Element ProjectNameElement = doc.createElement("projectName");
//                Element ProjectManagerElement = doc.createElement("projectManager");
//                Element DescriptionElement = doc.createElement("description");
//                Element NumofMinestoneElement = doc.createElement("numofMinestone");
//
//                Element StartDayElement = doc.createElement("startDay");
//                Element sdYearElement = doc.createElement("year");
//                Element sdMonthElement = doc.createElement("month");
//                Element sdDayElement = doc.createElement("day");
//
//                Element EndDayElement = doc.createElement("endDay");
//                Element seYearElement = doc.createElement("year");
//                Element seMonthElement = doc.createElement("month");
//                Element seDayElement = doc.createElement("day");
//
//                Element StatusElement = doc.createElement("status");
//
//                //Append text for element of project's info
//                ProjectIDElement.appendChild(doc.createTextNode(project.getProjectID() + ""));
//                ProjectNameElement.appendChild(doc.createTextNode(project.getProjectName() + ""));
//                ProjectManagerElement.appendChild(doc.createTextNode(project.getProjectManager() + ""));
//                DescriptionElement.appendChild(doc.createTextNode(project.getDescription() + ""));
//                NumofMinestoneElement.appendChild(doc.createTextNode(project.getNumOfMinestone() + ""));
//
//                sdYearElement.appendChild(doc.createTextNode(project.getStartDay().getYear() + ""));
//                sdMonthElement.appendChild(doc.createTextNode(project.getStartDay().getMonth() + ""));
//                sdDayElement.appendChild(doc.createTextNode(project.getStartDay().getDay() + ""));
//                StartDayElement.appendChild(sdYearElement);
//                StartDayElement.appendChild(sdMonthElement);
//                StartDayElement.appendChild(sdDayElement);
//
//                seYearElement.appendChild(doc.createTextNode(project.getEndDay().getYear() + ""));
//                seMonthElement.appendChild(doc.createTextNode(project.getEndDay().getMonth() + ""));
//                seDayElement.appendChild(doc.createTextNode(project.getEndDay().getDay() + ""));
//                EndDayElement.appendChild(seYearElement);
//                EndDayElement.appendChild(seMonthElement);
//                EndDayElement.appendChild(seDayElement);
//
//                StatusElement.appendChild(doc.createTextNode(project.getStatus() + ""));
//
//                //Append child
//                ProjectElement.appendChild(ProjectIDElement);
//                ProjectElement.appendChild(ProjectNameElement);
//                ProjectElement.appendChild(ProjectManagerElement);
//                ProjectElement.appendChild(DescriptionElement);
//                ProjectElement.appendChild(NumofMinestoneElement);
//                ProjectElement.appendChild(StartDayElement);
//                ProjectElement.appendChild(EndDayElement);
//                ProjectElement.appendChild(StatusElement);
//
//                //Append child for projects tag
//                projectsNode.appendChild(ProjectElement);
//                TransformerUtil.writeXML(doc, xmlFilePath);
//                projectIDList.add(project.getProjectID());
//            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllMinestonsOfProjectIDList(String xmlFilePath) {
        try {
            minestoneIDList = new ArrayList<Integer>();
            Minestones result = new Minestones();
            for (Iterator<Integer> it = projectIDList.iterator(); it.hasNext();) {
                ProjectDAO dao = new ProjectDAO();
                Integer integer = it.next();
                Minestones list = dao.GetAllMinestone(integer);

                for (int i = 0; i < list.size(); i++) {
                    result.add(list.get(i));
                    minestoneIDList.add(list.get(i).getMinestoneID());
                }
            }
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, result);

//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document doc = db.parse(new File(xmlFilePath));
//            Node minestonesNode = (doc.getElementsByTagName("minestones")).item(0);
//
//            //Delete old child
//            NodeList children = minestonesNode.getChildNodes();
//            for (int i = 0; i < children.getLength(); i++) {
//                if (children.item(i).getNodeName().equals("minestone")) {
//                    minestonesNode.removeChild(children.item(i));
//                }
//            }
//            minestoneIDList = new ArrayList<Integer>();
//            for (Iterator<Integer> it = projectIDList.iterator(); it.hasNext();) {
//                ProjectDAO dao = new ProjectDAO();
//                Integer integer = it.next();
//                Minestones list = dao.GetAllMinestone(integer);
//
//                //Write data into xml file
//                for (int i = 0; i < list.size(); i++) {
//                    Minestone mintstone = list.get(i);
//                    //Create element for project tag
//                    Element MinestoneElement = doc.createElement("minestone");
//                    Element MinestoneIDElement = doc.createElement("minestoneID");
//                    Element MinestoneNameElement = doc.createElement("minestoneName");
//                    Element ProjectIDElement = doc.createElement("projectID");
//                    Element DescriptionElement = doc.createElement("description");
//
//                    Element StartDayElement = doc.createElement("startDay");
//                    Element sdYearElement = doc.createElement("year");
//                    Element sdMonthElement = doc.createElement("month");
//                    Element sdDayElement = doc.createElement("day");
//
//                    Element EndDayElement = doc.createElement("endDay");
//                    Element seYearElement = doc.createElement("year");
//                    Element seMonthElement = doc.createElement("month");
//                    Element seDayElement = doc.createElement("day");
//
//                    Element StatusElement = doc.createElement("status");
//
//                    //Append text for element of project's info
//                    MinestoneIDElement.appendChild(doc.createTextNode(mintstone.getProjectID() + ""));
//                    MinestoneNameElement.appendChild(doc.createTextNode(mintstone.getMinestoneName() + ""));
//                    ProjectIDElement.appendChild(doc.createTextNode(mintstone.getProjectID() + ""));
//                    DescriptionElement.appendChild(doc.createTextNode(mintstone.getDescription() + ""));
//
//                    sdYearElement.appendChild(doc.createTextNode(mintstone.getStartDay().getYear() + ""));
//                    sdMonthElement.appendChild(doc.createTextNode(mintstone.getStartDay().getMonth() + ""));
//                    sdDayElement.appendChild(doc.createTextNode(mintstone.getStartDay().getDay() + ""));
//                    StartDayElement.appendChild(sdYearElement);
//                    StartDayElement.appendChild(sdMonthElement);
//                    StartDayElement.appendChild(sdDayElement);
//
//                    seYearElement.appendChild(doc.createTextNode(mintstone.getEndDay().getYear() + ""));
//                    seMonthElement.appendChild(doc.createTextNode(mintstone.getEndDay().getMonth() + ""));
//                    seDayElement.appendChild(doc.createTextNode(mintstone.getEndDay().getDay() + ""));
//                    EndDayElement.appendChild(seYearElement);
//                    EndDayElement.appendChild(seMonthElement);
//                    EndDayElement.appendChild(seDayElement);
//
//                    StatusElement.appendChild(doc.createTextNode(mintstone.getStatus() + ""));
//
//                    //Append child
//                    MinestoneElement.appendChild(MinestoneIDElement);
//                    MinestoneElement.appendChild(MinestoneNameElement);
//                    MinestoneElement.appendChild(ProjectIDElement);
//                    MinestoneElement.appendChild(DescriptionElement);
//                    MinestoneElement.appendChild(StartDayElement);
//                    MinestoneElement.appendChild(EndDayElement);
//                    MinestoneElement.appendChild(StatusElement);
//
//                    //Append child for projects tag
//                    minestonesNode.appendChild(MinestoneElement);
//                    minestoneIDList.add(mintstone.getMinestoneID());
//                }
//            }
//            TransformerUtil.writeXML(doc, xmlFilePath);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllMembersOfProjectIDList(String xmlFilePath) {
        try {
            Members result = new Members();
            for (Iterator<Integer> it = projectIDList.iterator(); it.hasNext();) {
                ProjectDAO dao = new ProjectDAO();
                Integer integer = it.next();
                Members list = dao.GetAllMembersOfProject(integer);

                for (int i = 0; i < list.size(); i++) {
                    result.add(list.get(i));
                }
            }
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, result);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllMinestonsOfProjectID(String xmlFilePath, Integer projectID) {
        try {
            minestoneIDList = new ArrayList<Integer>();
            Minestones result = new Minestones();
            ProjectDAO dao = new ProjectDAO();
            result = dao.GetAllMinestone(projectID);

            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, result);

//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            DocumentBuilder db = dbf.newDocumentBuilder();
//            Document doc = db.parse(new File(xmlFilePath));
//            Node minestonesNode = (doc.getElementsByTagName("minestones")).item(0);
//
//            //Delete old child
//            NodeList children = minestonesNode.getChildNodes();
//            for (int i = 0; i < children.getLength(); i++) {
//                if (children.item(i).getNodeName().equals("minestone")) {
//                    minestonesNode.removeChild(children.item(i));
//                }
//            }
//            minestoneIDList = new ArrayList<Integer>();
//            for (Iterator<Integer> it = projectIDList.iterator(); it.hasNext();) {
//                ProjectDAO dao = new ProjectDAO();
//                Integer integer = it.next();
//                Minestones list = dao.GetAllMinestone(integer);
//
//                //Write data into xml file
//                for (int i = 0; i < list.size(); i++) {
//                    Minestone mintstone = list.get(i);
//                    //Create element for project tag
//                    Element MinestoneElement = doc.createElement("minestone");
//                    Element MinestoneIDElement = doc.createElement("minestoneID");
//                    Element MinestoneNameElement = doc.createElement("minestoneName");
//                    Element ProjectIDElement = doc.createElement("projectID");
//                    Element DescriptionElement = doc.createElement("description");
//
//                    Element StartDayElement = doc.createElement("startDay");
//                    Element sdYearElement = doc.createElement("year");
//                    Element sdMonthElement = doc.createElement("month");
//                    Element sdDayElement = doc.createElement("day");
//
//                    Element EndDayElement = doc.createElement("endDay");
//                    Element seYearElement = doc.createElement("year");
//                    Element seMonthElement = doc.createElement("month");
//                    Element seDayElement = doc.createElement("day");
//
//                    Element StatusElement = doc.createElement("status");
//
//                    //Append text for element of project's info
//                    MinestoneIDElement.appendChild(doc.createTextNode(mintstone.getProjectID() + ""));
//                    MinestoneNameElement.appendChild(doc.createTextNode(mintstone.getMinestoneName() + ""));
//                    ProjectIDElement.appendChild(doc.createTextNode(mintstone.getProjectID() + ""));
//                    DescriptionElement.appendChild(doc.createTextNode(mintstone.getDescription() + ""));
//
//                    sdYearElement.appendChild(doc.createTextNode(mintstone.getStartDay().getYear() + ""));
//                    sdMonthElement.appendChild(doc.createTextNode(mintstone.getStartDay().getMonth() + ""));
//                    sdDayElement.appendChild(doc.createTextNode(mintstone.getStartDay().getDay() + ""));
//                    StartDayElement.appendChild(sdYearElement);
//                    StartDayElement.appendChild(sdMonthElement);
//                    StartDayElement.appendChild(sdDayElement);
//
//                    seYearElement.appendChild(doc.createTextNode(mintstone.getEndDay().getYear() + ""));
//                    seMonthElement.appendChild(doc.createTextNode(mintstone.getEndDay().getMonth() + ""));
//                    seDayElement.appendChild(doc.createTextNode(mintstone.getEndDay().getDay() + ""));
//                    EndDayElement.appendChild(seYearElement);
//                    EndDayElement.appendChild(seMonthElement);
//                    EndDayElement.appendChild(seDayElement);
//
//                    StatusElement.appendChild(doc.createTextNode(mintstone.getStatus() + ""));
//
//                    //Append child
//                    MinestoneElement.appendChild(MinestoneIDElement);
//                    MinestoneElement.appendChild(MinestoneNameElement);
//                    MinestoneElement.appendChild(ProjectIDElement);
//                    MinestoneElement.appendChild(DescriptionElement);
//                    MinestoneElement.appendChild(StartDayElement);
//                    MinestoneElement.appendChild(EndDayElement);
//                    MinestoneElement.appendChild(StatusElement);
//
//                    //Append child for projects tag
//                    minestonesNode.appendChild(MinestoneElement);
//                    minestoneIDList.add(mintstone.getMinestoneID());
//                }
//            }
//            TransformerUtil.writeXML(doc, xmlFilePath);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllTasksOfMinestoneIDList(String xmlFilePath) {
        try {
            Tasks tasks = new Tasks();
            ProjectDAO dao = new ProjectDAO();
            for (Iterator<Integer> it = minestoneIDList.iterator(); it.hasNext();) {
                Integer integer = it.next();
                Tasks list = dao.GetAllTasksbyMinestoneID(integer);
                for (int i = 0; i < list.size(); i++) {
                    Task object = list.get(i);
                    tasks.add(object);
                }
            }
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, tasks);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllTasksOfMinestoneID(String xmlFilePath, Integer minestoneID) {
        try {
            Tasks tasks = new Tasks();
            ProjectDAO dao = new ProjectDAO();
            tasks = dao.GetAllTasksbyMinestoneID(minestoneID);
            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, tasks);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void getAllMinestoneOfUsername(String xmlFilePath, String username) {
        try {
            ProjectDAO dao = new ProjectDAO();
            Minestones minestones = dao.GetAllMinestoneOfUsername(username);

            SupportUtil util = new SupportUtil();
            util.marshal(xmlFilePath, minestones);

        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
